//
//  HomeTableViewController.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/8.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
private let homeCellId = "homeCellId"

class HomeTableViewController: BaseTableViewController {
    
    private lazy var statuses = [Status]()

    override func viewDidLoad() {
        super.viewDidLoad()        
        
        if !userLogin {
            
            visitorLoginView?.setUIInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            
            return
        }
        pareparTableView()
        loadData()
    }
    
    func loadData(){
        
        //如果max_id 和since_id同时有值,服务器不会返回任何数据
        var since_id = 0
        var max_id = 0
        
        if indicatorView.isAnimating(){
            
            since_id = 0
            max_id = statuses.last?.id ?? 0
            
        }else{
            max_id = 0
            since_id = statuses.first?.id ?? 0

        }
        
//        let since_id = statuses.first?.id ?? 0
//        let max_id = statuses.last?.id ?? 0
        
        StatusListViewModel.loadData(since_id: since_id, max_id: max_id) { (array) -> () in
            self.refreshControl?.endRefreshing()
            
            if array == nil {
                
                SVProgressHUD.showErrorWithStatus("您的网络出错啦...")
                
                self.indicatorView.stopAnimating()
                return
            }
            
            if since_id > 0{
                //下拉刷新.直接追加数据
                self.statuses = array! + self.statuses
            }else if max_id > 0{
                //上拉刷新
                self.statuses += array!
                self.indicatorView.stopAnimating()
            }else{
                //首次加载
                self.statuses = array!
            }
            
            
            self.tableView.reloadData()
        }
    }
    //注册行
    func pareparTableView(){
        
        refreshControl = UIRefreshControl()
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: homeCellId)
        tableView.estimatedRowHeight = 250
        tableView.rowHeight = UITableViewAutomaticDimension
       
        
        tableView.separatorStyle = .None
        tableView.tableFooterView = indicatorView
        
    }
    //增加下拉刷新的小菊花
    private lazy var indicatorView : UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        
        return indicator
    
    }()

}

extension HomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuses.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath) as! StatusCell
        
        cell.statues = statuses[indexPath.row]
        
        //倒数第二行并且小菊花没有转动的时候加载更多数据
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating(){
            
            indicatorView.startAnimating()
            
             loadData()
        }
       
        
        return cell
    }
    
    
}
